import java.io.*;
import java.net.*;
import java.lang.Integer;

public class KunalClient{
	
  	String SERVER_HOST = new String("localhost");
	int SERVER_PORT = 0;
	InetAddress serverInetAddress;
	Socket serverSocketAddress;
	BufferedReader mySocketReader,myInputReader;
	PrintWriter mySocketWriter;
	
	
	
  public KunalClient() throws Exception
  {     //Initialises all the varibales that would be required...
	    
		//System.out.println("Please enter Server port # : ");
		myInputReader = new BufferedReader( new InputStreamReader (System.in));
		//SERVER_PORT = Integer.parseInt( new String( myInputStreamReader.readLine() ));
	    SERVER_PORT = 15687;
	    serverInetAddress = InetAddress.getByName(SERVER_HOST);
	    serverSocketAddress = new Socket( serverInetAddress , SERVER_PORT);
	    mySocketReader = new BufferedReader(new InputStreamReader( serverSocketAddress.getInputStream()));
	    mySocketWriter = new PrintWriter(new OutputStreamWriter( serverSocketAddress.getOutputStream()));
	
  }// End constructor KunalClient()

  public void establishConnection() throws Exception
  {
	mySocketWriter.println("Connection Request");
	mySocketWriter.flush();
	
  }// End establishConnection

  public boolean getConnectionConfirmation() throws Exception
  {
	String confirmation = new String(mySocketReader.readLine());
	System.out.println("Connection :: " + confirmation); 
	if(confirmation.equalsIgnoreCase("QUEUED"))
	 {
		 return false;
     }
	
	return true;
  }// End getConnectionConfirmation()

  public void login() throws Exception
  {
     // Sends "Login Name to Server"
        String loginName;
        System.out.println("Please Enter - Login Name : ");
        loginName = new String( myInputReader.readLine());
     	mySocketWriter.println(loginName);
	    mySocketWriter.flush();
     	
  }// End Llogin()

  public void getServerServices() throws Exception
  { // Reading menu
	boolean lastLine = false;
	String services = new String("");
	while( !lastLine )
	{ 
	  services = mySocketReader.readLine();
	  lastLine = services.equalsIgnoreCase("flagLastLine");
	  if (!lastLine)
	  {
		System.out.println(services);
	  }
    }
  }// End getServerServices()

  public int requestService() throws Exception
  {
	//Sends service Number
	//System.out.println("Inside requestService\n");
	int serviceNumber = 0;
	serviceNumber = Integer.parseInt(new String(myInputReader.readLine()));
	//System.out.println("Client reuqested : " + serviceNumber );
	mySocketWriter.println(serviceNumber);
	mySocketWriter.flush();
	return serviceNumber;
	
  }// End requestService()  

  public void waitForServer() throws Exception
  {
	System.out.println(new String(mySocketReader.readLine()));
	
	
	
  }// End waitForServer()

  public void receiveService(int serviceNumber) throws Exception
  {
		int service = serviceNumber;
		String accountName = new String();
		//System.out.println("Service Case Number" + service);
		switch(service)
		{
		    case 1:{ // Creating an Account
			         System.out.println("Enter Account Name");
			         String serverText = new String(mySocketReader.readLine());
			         System.out.println(serverText);
			         accountName = myInputReader.readLine();
			         mySocketWriter.println(accountName);
			         mySocketWriter.flush();
			         System.out.println("Exiting the service");
			         break;   
		           }
		  
		    case 2:{ // List of Valid Accounts
			
			         break;
		           }
		 
		    case 3:{ // List of current Online Users
			
			         break;
		           }
		
		    case 4:{ // Add Chat Room
			
			         break;
		           }
		
		    case 5:{  // Join Group Chat
			
			         break;
		           }
		   
		    case 6:{  System.out.println("Service 6 : To be Implemented..");
			
			         break;
		           }
		
			
			default: System.out.println("Invalid Selection :");
			          break;
			
			
		}// End switch()
	
	
  }// End receiveService()

  public void closeAllConnections() throws Exception
  {
	serverSocketAddress.close();
	
  }// End closeAllConnections()

  

  public static void main(String[] args) throws Exception {
	
	  
	  KunalClient myClient = new KunalClient();
	  myClient.establishConnection();
	  if(myClient.getConnectionConfirmation())
	  {	
	    myClient.login();
	    myClient.getServerServices();
	    int serviceNumber = myClient.requestService();
	
	    myClient.receiveService(serviceNumber);
	
	    
      }else{
	    myClient.waitForServer();
	    myClient.login();
	    myClient.getServerServices();
	    myClient.requestService();
	
      }
      


      myClient.closeAllConnections();
  }

}